package artid.com.arttogo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.ReportActivity;
import artid.com.arttogo.app.ArtToGoApp;
import artid.com.arttogo.bean.AuctionCommentParser;
import artid.com.arttogo.bean.OneStateIntParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.utils.ToastUtil;
import artid.com.arttogo.view.ChangeColorTextView;
import artid.com.arttogo.view.CircleImageView;

import static anet.channel.util.Utils.context;
import static artid.com.arttogo.app.ArtToGoApp.getActivity;
import static artid.com.arttogo.network.AppUtil.reportAndCollectionType;
import static java.lang.Integer.parseInt;

/**
 * 项目名称：ArtworkArt
 * 类名称：AuctionComment_Adapter
 * 类描述：评论适配器
 * 创建人：耿卫斌
 * 创建时间：2017-3-6 下午5:15:43
 */
public class AuctionComment_Adapter extends BaseAdapter {
    private Context mContext;
    private String Type;//请求评论列表使用的类型	type	1,机构，2，拍卖会，3专场，4拍品
    private List<AuctionCommentParser> list;

    public AuctionComment_Adapter(Context mContext, String type, List<AuctionCommentParser> list) {
        this.mContext = mContext;
        Type = type;
        this.list = list;
    }

    public void notifyData(List<AuctionCommentParser> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = LayoutInflater.from(mContext).inflate(R.layout.item_auctioncomment, null);
            holder.head_img = (CircleImageView) v.findViewById(R.id.itemAuctionComment_headimg);
            holder.name = (TextView) v.findViewById(R.id.itemAuctionComment_name);
            holder.time = (TextView) v.findViewById(R.id.itemAuctionComment_time);
            holder.mRatingBar = (RatingBar) v.findViewById(R.id.itemAuctionComment_ratingBar);
            holder.content = (ChangeColorTextView) v.findViewById(R.id.itemAuctionComment_content);
            holder.fabulousBtn = (TextView) v.findViewById(R.id.itemAuctionComment_fabulousBtn);
            holder.replyBtn = (TextView) v.findViewById(R.id.itemAuctionComment_replyBtn);
            holder.deleteBtn = (TextView) v.findViewById(R.id.itemAuctionComment_deleteBtn);
            holder.reportBtn = (TextView) v.findViewById(R.id.itemAuctionComment_reportBtn);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.name.setText(list.get(position).getRealname());
        Glide.with(mContext).load(list.get(position).getImgurl()
                + "_" + (parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH)) / 3) + "x10.png"
        ).asBitmap().centerCrop()
                .placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate)
                .into(new MyBitmapImageViewTarget(holder.head_img));
        holder.mRatingBar.setRating(list.get(position).getScore());
        int status = list.get(position).getStatus();
        switch (status) {
            case 0:
                //0:表示评论
                holder.content.setSpecifiedTextsColor(list.get(position).getReplyname() + list.get(position).getContent(), "@" + list.get(position).getReplyname(), Color.parseColor("#6EA1DA"));
                break;
            case 1:
                //1表示回复
                holder.content.setSpecifiedTextsColor("回复@" + list.get(position).getReplyname() + list.get(position).getContent(), "@" + list.get(position).getReplyname(), Color.parseColor("#6EA1DA"));
                break;

            default:
                break;
        }
        holder.time.setText(" " + list.get(position).getCreate_time());

        final int is_commend = list.get(position).getIs_commend();
        //初始化品论样式
        if (is_commend == 0) {
            //未评论
            setTextViewDrawable(holder.fabulousBtn, false);
        } else {
            //已评论
            setTextViewDrawable(holder.fabulousBtn, true);
        }
        if (AppUtil.getUser() != null || list!=null) {
            if (!AppUtil.getUser().getUid().equals(list.get(position).getUid())) {
                //判断当前评论是否是本人评论的如果是显示删除按钮反之不显示
                holder.deleteBtn.setVisibility(View.GONE);
                holder.reportBtn.setVisibility(View.VISIBLE);
            } else {
                holder.deleteBtn.setVisibility(View.VISIBLE);
                holder.reportBtn.setVisibility(View.GONE);
            }
        } else {
            holder.deleteBtn.setVisibility(View.GONE);
            holder.reportBtn.setVisibility(View.VISIBLE);
        }

        //点赞
        holder.fabulousBtn.setText(" " + list.get(position).getComNum());
        holder.fabulousBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).getIs_commend() == 0) {
                    commentFabulous(list.get(position).getId(), "1", v, position);
                } else {
                    commentFabulous(list.get(position).getId(), "2", v, position);
                }
            }
        });

        //回复评论
        holder.replyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppUtil.LoginVer(getActivity())) {
                    return;
                }
                //评论列表
                Intent intent0 = new Intent("com.AuctionComment_Activity.receiver");
                intent0.putExtra("isReply", true);
                intent0.putExtra("Position", position);
                mContext.sendBroadcast(intent0);
                //机构详情评论
                Intent intent1 = new Intent("com.AucOrgDetActivity.receiver");
                intent1.putExtra("isReply", true);
                intent1.putExtra("Position", position);
                mContext.sendBroadcast(intent1);
                //拍品详情评论
                Intent intent2 = new Intent("com.LotDetailFragment.receiver");
                intent2.putExtra("isReply", true);
                intent2.putExtra("Position", position);
                mContext.sendBroadcast(intent2);
            }
        });

        //删除评论
        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppUtil.LoginVer(getActivity())) {
                    return;
                }
                deleteComment(position);
            }
        });

        //举报
        holder.reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //举报
                if (AppUtil.LoginVer(getActivity())) {
                    return;
                }
                Intent intent = new Intent(mContext, ReportActivity.class);
                intent.putExtra("id", list.get(position).getId());
                intent.putExtra("type", "5");
                intent.putExtra("intentType", -2);
                mContext.startActivity(intent);
            }
        });
        return v;
    }

    class ViewHolder {
        CircleImageView head_img;
        TextView name, time, fabulousBtn, replyBtn, deleteBtn, reportBtn;
        RatingBar mRatingBar;
        ChangeColorTextView content;
    }

    /*
    * 评论
    * ID 评论id
    * state 1：设置点赞  2：取消点赞
    * 类型	type	1,机构，2，拍卖会，3专场，4拍品评论,5拍品
    */
    private void commentFabulous(String id, final String state, final View v, final int position) {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.commentSupport,
                VolleyMap.getInstance().commentSupport(id, state, Type),
                OneStateIntParser.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        if (vd.getCode() == -99) {
                            AppUtil.Logout(getActivity());
                            AppUtil.LoginVer(getActivity());
                            return;
                        }
                        OneStateIntParser bean = (OneStateIntParser) vd.getData();
                        if (bean.getState() == 1) {
                            int comNum = Integer.parseInt(list.get(position).getComNum());//点赞数量
                            if ("1".equals(state)) {
                                comNum = comNum + 1;
                                list.get(position).setIs_commend(1);
                                list.get(position).setComNum(comNum + "");
                                setTextViewDrawable((TextView) v, true);
                            } else {
                                comNum = comNum - 1;
                                list.get(position).setIs_commend(0);
                                list.get(position).setComNum(comNum + "");
                                setTextViewDrawable((TextView) v, false);
                            }
                            notifyDataSetChanged();
                        } else {
                            ToastUtil.toast(mContext, "点赞失败");
                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }


    /*
     * 删除评论
	 * ID 评论id
	 * 类型	type	1,机构，2，拍卖会，3专场，4拍品
	 */
    private void deleteComment(final int position) {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.delComment,
                VolleyMap.getInstance().delComment(list.get(position).getMessid(), Type),
                OneStateIntParser.class, new VolleyCallback() {

                    @SuppressWarnings({"unchecked", "static-access"})
                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        OneStateIntParser bean = (OneStateIntParser) vd.getData();
                        if (bean.getState() == 1) {
                            list.remove(position);
                            notifyDataSetChanged();
                            ToastUtil.toast(mContext, "删除成功");
                        } else {
                            ToastUtil.toast(mContext, "删除失败");
                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                        ToastUtil.toast(mContext, "删除失败");
                    }
                });
    }

    /**
     * 动态设置TextViewDrawable
     *
     * @param v
     */
    public void setTextViewDrawable(TextView v, Boolean isFabulous) {
        Drawable drawable;
        if (isFabulous) {
            //判断是否点赞
            drawable = mContext.getResources().getDrawable(R.drawable.jg_xq_good_sel);
        } else {
            drawable = mContext.getResources().getDrawable(R.drawable.jg_xq_good_nor );
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
        v.setCompoundDrawables(drawable, null, null, null);//画在左边
    }

}
